import WxValidate from '../../utils/WxValidate.js'

Page({
    data: {
        CNCompany: '',
        Street: '',
        ZipCode: '',
        CompanyContact: '',
        CompanyContactSex: '',
        CompanyContactMobile: '',
        CompanyContactEmail: '',
        CompanyContactAttend: '否',
        invoice: '',
        invoiceNo: '',
        invoiceContent: '',
        remark: '',
        assistance: false,
        list: []
    },
    onLoad() {
        wx.showLoading({
            title: '加载中'
        })

        wx.showShareMenu({
            withShareTicket: true
        })

        this.initValidate()
    },
    onReady() {
        wx.hideLoading()
    },
    changeData(ev) {
        const name = ev.currentTarget.dataset.name
        let data = ev.detail
        let list = this.data.list

        if (typeof data == 'string') {
            data = data.trim()
        }

        this.setData({
            [name]: data
        })

        if (name == 'CompanyContactAttend') {
            if (data == '是') {
                list.unshift({
                    InnerUser: this.data.CompanyContact,
                    InnerSex: this.data.CompanyContactSex,
                    InnerDept: '',
                    InnerJob: '',
                    InnerMobile: this.data.CompanyContactMobile,
                    InnerEmail: this.data.CompanyContactEmail,
                    InnerVipStat: '',
                    InnerStudentStat: ''
                })

                wx.showToast({
                    title: '添加成功'
                })
            } else {
                list.shift()

                wx.showToast({
                    title: '删除成功'
                })
            }
        }

        if (this.data.CompanyContactAttend == '是') {
            switch (name) {
                case 'CompanyContact':
                    list[0].InnerUser = this.data.CompanyContact
                    break
                case 'CompanyContactSex':
                    list[0].InnerSex = this.data.CompanyContactSex
                    break
                case 'CompanyContactMobile':
                    list[0].InnerMobile = this.data.CompanyContactMobile
                    break
                case 'CompanyContactEmail':
                    list[0].InnerEmail = this.data.CompanyContactEmail
            }
        }

        this.setData({
            list: list
        })
    },
    addList() {
        let list = this.data.list
        list.push({
            InnerUser: '',
            InnerSex: '',
            InnerDept: '',
            InnerJob: '',
            InnerMobile: '',
            InnerEmail: '',
            InnerVipStat: '',
            InnerStudentStat: ''
        })

        this.setData({
            list: list
        })

        wx.showToast({
            title: '添加成功'
        })
    },
    delList(ev) {
        wx.showModal({
            content: '确认删除此条信息？',
            success: res => {
                if (res.confirm) {
                    const index = ev.currentTarget.dataset.index
                    let list = this.data.list
                    list.splice(index, 1)

                    this.setData({
                        list: list
                    })

                    wx.showToast({
                        title: '删除成功'
                    })
                }
            }
        })
    },
    initValidate() {
        const rules = {
            CNCompany: {
                required: true
            },
            Street: {
                required: true
            },
            CompanyContact: {
                required: true
            },
            CompanyContactSex: {
                required: true
            },
            CompanyContactMobile: {
                required: true,
                phone: true
            },
            CompanyContactEmail: {
                required: true,
                email: true
            },
            InnerUser: {
                required: true
            },
            InnerSex: {
                required: true
            },
            InnerMobile: {
                required: true,
                phone: true
            },
            InnerEmail: {
                required: true,
                email: true
            },
            InnerVipStat: {
                required: true
            },
            InnerStudentStat: {
                required: true
            }
        }

        const messages = {
            CNCompany: {
                required: '请输入单位名称'
            },
            Street: {
                required: '请输入单位通讯地址'
            },
            CompanyContact: {
                required: '请输入联系人'
            },
            CompanyContactSex: {
                required: ' 请选择性别'
            },
            CompanyContactMobile: {
                required: '请输入联系电话'
            },
            CompanyContactEmail: {
                required: '请输入Email地址',
                email: '请输入有效的Email地址'
            },
            InnerUser: {
                required: '请输入参会人员姓名'
            },
            InnerSex: {
                required: '请选择参会人员性别'
            },
            InnerMobile: {
                required: '请输入参会人员联系电话'
            },
            InnerEmail: {
                required: '请输入参会人员Email地址',
                email: '请输入有效的Email地址'
            },
            InnerVipStat: {
                required: '请选择参会人员是否为学会会员'
            },
            InnerStudentStat: {
                required: '请选择参会人员是否为学生'
            }
        }

        this.WxValidate = new WxValidate(rules, messages)

        this.WxValidate.addMethod('phone', (value, param) => {
            const reg = /^1[3-9]\d{9}$/
            return reg.test(value)
        }, '请输入有效的联系电话')
    },
    isRepeat(key) {
        const list = this.data.list
        let tempArr = []

        for (let i = 0; i < list.length; i++) {
            if (!tempArr.includes(list[i][key])) {
                tempArr.push(list[i][key])
            } else {
                return {
                    success: false,
                    number: list[i][key]
                }
            }
        }

        return {
            success: true
        }
    },
    formSubmit() {
        const formData = JSON.parse(JSON.stringify(this.data))
        const list = formData.list
        delete formData.list

        for (let i = 0; i < list.length; i++) {
            Object.assign(formData, list[i])

            if (!this.WxValidate.checkForm(formData)) {
                const error = this.WxValidate.errorList[0]

                wx.showModal({
                    content: error.msg,
                    showCancel: false
                })

                return false
            }
        }

        if (list.length) {
            if (!this.isRepeat('InnerMobile').success) {
                wx.showModal({
                    content: '参会人员联系电话' + this.isRepeat('InnerMobile').number + '已存在',
                    showCancel: false
                })

                return false
            }

            if (!this.isRepeat('InnerEmail').success) {
                wx.showModal({
                    content: '参会人员Email地址' + this.isRepeat('InnerEmail').number + '已存在',
                    showCancel: false
                })

                return false
            }
        }

        wx.showLoading({
            title: '请稍等'
        })

        let checkRepeat = []

        for (let i = 0; i < list.length; i++) {
            checkRepeat.push(new Promise((resolve, reject) => {
                wx.request({
                    url: 'https://www.kelionline.com/topic_owner/mnm/chk/InnerMobile/' + list[i].InnerMobile,
                    method: 'GET',
                    success: res => {
                        resolve({
                            content: res.data == 'exist' ? '参会人员联系电话' + list[i].InnerMobile + '已存在' : ''
                        })
                    }
                })
            }))

            checkRepeat.push(new Promise((resolve, reject) => {
                wx.request({
                    url: 'https://www.kelionline.com/topic_owner/mnm/chk/InnerEmail/' + list[i].InnerEmail,
                    method: 'GET',
                    success: res => {
                        resolve({
                            content: res.data == 'exist' ? '参会人员Email地址' + list[i].InnerEmail + '已存在' : ''
                        })
                    }
                })
            }))
        }

        Promise.all(checkRepeat).then(res => {
            if (res.some(item => item.content != '')) {
                const msg = res.find(item => item.content != '').content
                wx.hideLoading()

                wx.showModal({
                    content: msg,
                    showCancel: false
                })
            } else {
                this.data.assistance = this.data.assistance ? '是' : '否'

                wx.request({
                    url: 'https://www.kelionline.com/api/miniProgram/mnm/enroll/save',
                    data: this.data,
                    method: 'POST',
                    success: res => {
                        wx.hideLoading()

                        if (res.data.code == 0 && res.data.data == 'succ') {
                            wx.showModal({
                                content: '报名成功',
                                showCancel: false,
                                complete: () => {
                                    this.setData({
                                        CNCompany: '',
                                        Street: '',
                                        ZipCode: '',
                                        CompanyContact: '',
                                        CompanyContactSex: '',
                                        CompanyContactMobile: '',
                                        CompanyContactEmail: '',
                                        CompanyContactAttend: '',
                                        invoice: '',
                                        invoiceNo: '',
                                        invoiceContent: '',
                                        remark: '',
                                        assistance: false,
                                        list: []
                                    })

                                    wx.switchTab({
                                        url: '/pages/index/index'
                                    })
                                }
                            })
                        } else {
                            wx.showModal({
                                content: '报名失败',
                                showCancel: false
                            })
                        }
                    }
                })
            }
        })
    }
})